package com.taptech.doufu.ui.view.drawcircle;

import android.content.Context;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;


import com.facebook.drawee.view.SimpleDraweeView;
import com.taptech.doufu.R;
import com.taptech.doufu.bean.ImageBean;
import com.taptech.doufu.util.DiaobaoUtil;
import com.taptech.doufu.util.ImageManager;

import java.util.List;

/**
 * Created by lsy on 2015/7/7.
 */
public class PersonalNoteImgListViewAdapter extends BaseAdapter {


    public PersonalNoteImgListViewAdapter(Context context, List<ImageBean> dataList) {
        this.mContext = context;
        this.mDataList = dataList;
    }

    private Context mContext;
    private List<ImageBean> mDataList;

    @Override
    public int getCount() {

        if (mDataList != null) {
            return mDataList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {
        final ImgViewHoler viewHoler;
        if (view == null) {
            view = LayoutInflater.from(mContext).inflate(R.layout.draw_img_item_display, null);
            view.setLayerType(View.LAYER_TYPE_NONE, null);
            viewHoler = new ImgViewHoler();
            viewHoler.imageView = (SimpleDraweeView) view.findViewById(R.id.draw_img_item);
            viewHoler.mGifSignIcon = (ImageView) view.findViewById(R.id.image_gif_sign_icon);
            view.setTag(viewHoler);
        } else {
            viewHoler = (ImgViewHoler) view.getTag();
        }
        try {
            String headUrl = "http://imgold2.doufu.la/";

            String imgUrl = headUrl + (mDataList.get(position).getThumbnail_path());
            String lowImgUrl = headUrl + (mDataList.get(position).getThumbnail2_path());
            int cropImgWidth = Integer.valueOf(mDataList.get(position).getThumbnail_width());
            int cropImgHeight = Integer.valueOf(mDataList.get(position).getThumbnail_height());
            float imageAspectRatio = cropImgWidth / (float) cropImgHeight;
            ImageManager.loadImage(viewHoler.imageView, imgUrl, lowImgUrl, imageAspectRatio);
            // 如果是gif则显示标志
            if (DiaobaoUtil.isImageUrlGif(imgUrl)) {
                viewHoler.mGifSignIcon.setVisibility(View.VISIBLE);
            } else {
                viewHoler.mGifSignIcon.setVisibility(View.GONE);
            }
        } catch (Exception e) {

        }
        return view;
    }

    class ImgViewHoler {
        public SimpleDraweeView imageView;
        // gif标志
        public ImageView mGifSignIcon;
    }
}
